package com.electric.qrapp.utils.function

import android.Manifest
import androidx.fragment.app.FragmentActivity
import androidx.lifecycle.MutableLiveData
import com.electric.qrapp.utils.system.showToast
import com.permissionx.guolindev.PermissionX

/**
 *@author created by WA03675
 *@date  2022-07-14 11:06
 *@des
 */


fun albumPermission(activity: FragmentActivity, resultListener: MutableLiveData<Boolean>) {
    PermissionX.init(activity)
        .permissions(
            Manifest.permission.CAMERA,
            Manifest.permission.READ_EXTERNAL_STORAGE,
            Manifest.permission.WRITE_EXTERNAL_STORAGE
        )
        .onExplainRequestReason { scope, deniedList ->
            scope.showRequestReasonDialog(
                deniedList,
                "您必须先同意此权限才能上传图片",
                "好的",
                "取消"
            )

        }.request { allGranted, _, _ ->
            resultListener.postValue(allGranted)
            if (allGranted) {
            } else {
                showToast(msg = "权限被拒绝，无法启动扫码服务")
            }
        }
}

fun filePermission(activity: FragmentActivity, resultListener: MutableLiveData<Boolean>) {
    PermissionX.init(activity)
        .permissions(
            Manifest.permission.READ_EXTERNAL_STORAGE,
            Manifest.permission.WRITE_EXTERNAL_STORAGE
        )
        .onExplainRequestReason { scope, deniedList ->
            scope.showRequestReasonDialog(
                deniedList,
                "您必须先同意此权限才能使用此功能",
                "好的",
                "取消"
            )

        }.request { allGranted, _, _ ->
            resultListener.postValue(allGranted)
            if (allGranted) {
            } else {
                showToast(msg = "权限被拒绝，无法启动扫码服务")
            }
        }
}